%Figure 6
%Plot solution over time with and without loan portfolios
clear all; close all; 


%Solution with Loan Portfolios, i.e., two loans
para;
%Change parameters relative to baseline
kappa=11;
phi=11;
aBar=0.2;
T=25;

LoanPortfolio;
solVTimePortfolio=ode15s(@OdeTimePortfolio,[0 T],kappa*q,options,sol.x,vdV );

%Calculate outcomes as a function of time t
for t=1:length(solVTimePortfolio.x)

    Vt=solVTimePortfolio.y(t);
    [m,ind]=min(abs(Vt-sol.x));
    vAt(t)=vA(ind);
    vFt(t)=sol.y(1,ind);

end


%Plot
close all;
fFigure=figure(1); hold on;

 subplot(1,3,2); hold on
 plot(solVTimePortfolio.x,vAt, 'k', 'Linewidth',3);
 leg2=legend('$V_0$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$V$','Interpreter','latex')
 ylabel('$F(V)$','Interpreter','latex')
 title('A: Value Function','Interpreter', 'LaTex', 'FontSize',13)

 subplot(1,3,1); hold on
 plot(solVTimePortfolio.x,solVTimePortfolio.y, 'k', 'Linewidth',3);
 leg2=legend('$V_0$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$V$','Interpreter','latex')
 ylabel('$F(V)$','Interpreter','latex')
 title('A: Value Function','Interpreter', 'LaTex', 'FontSize',13)

  subplot(1,3,3); hold on
 plot(solVTimePortfolio.x,vFt/2, 'k', 'Linewidth',3);
 leg2=legend('$V_0$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$V$','Interpreter','latex')
 ylabel('$F(V)$','Interpreter','latex')
 title('A: Value Function','Interpreter', 'LaTex', 'FontSize',13)


 %Solution without Loan Portfolios, i.e., with single loan
clear all;
para;
options = odeset('AbsTol',1e-13,'RelTol',1e-13);

kappa=11;
phi=11;
OptContract;

SB;

Solve;

T=25;
Time;
fFigure=figure(1); hold on;


subplot(1,3,2); hold on
 plot(solVTime.x,smooth(vAt), 'r:', 'Linewidth',3);
leg2=legend('Portfolio','Baseline','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',13);
 xlabel('$t$','Interpreter','latex')
 ylabel('$a_t$','Interpreter','latex')
 title('B: Monitoring Effort','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

   subplot(1,3,1); hold on
 plot(solVTime.x,solVTime.y(1,:), 'r:', 'Linewidth',3);
leg2=legend('Portfolio','Baseline','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',13);
 xlabel('$t$','Interpreter','latex')
 ylabel('$V_t$','Interpreter','latex')
 title('A: Screening Incentives','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

  subplot(1,3,3); hold on
 plot(solVTime.x,solVTime.y(2,:), 'r:', 'Linewidth',3);
leg2=legend('Portfolio','Baseline','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',13);
 xlabel('$t$','Interpreter','latex')
 ylabel('$F_t$','Interpreter','latex')
 title('C: Value Function','Interpreter', 'LaTex', 'FontSize',13)
axis tight;




resize_plot(fFigure,12.5,2.75,13);
print('TimeDynamicsPortfolio','-depsc')

